package com.easou.ps.exec;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorBuilder<T> {
	private int coreSize = 10;
	private int maxSize = 20;
	private int queueSize = 10000;
	private int idleMins = 1;
	private int timeoutMills = 5000;
	private String executorName = "";
	
	public ExecutorBuilder() {
	}
	
	public ExecutorBuilder(String executorName) {
		this.executorName = executorName;
	}

	public ExecutorBuilder<T> setCoreSize(int coreSize) {
		this.coreSize = coreSize;
		return this;
	}

	public ExecutorBuilder<T> setMaxSize(int maxSize) {
		this.maxSize = maxSize;
		return this;
	}

	public ExecutorBuilder<T> setQueueSize(int queueSize) {
		this.queueSize = queueSize;
		return this;
	}

	public ExecutorBuilder<T> setIdleTime(int minutes) {
		this.idleMins = minutes;
		return this;
	}

	public ExecutorBuilder<T> setTimeout(int mills) {
		this.timeoutMills = mills;
		return this;
	}
	
	public ExecutorBuilder<T> setExecutorName(String executorName) {
		this.executorName = executorName;
		return this;
	}

	public Executor<T> toExecutor() {
		int maxSize = this.maxSize > coreSize ? this.maxSize : coreSize;
		ThreadPoolExecutor exec = new ThreadPoolExecutor(coreSize, maxSize, idleMins, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(queueSize));
		return new Executor<T>(executorName, exec, timeoutMills);
	}

}
